package cfengMiddleware.server.rabbitmq.consumer;

import cfengMiddleware.server.rabbitmq.entity.MessageEntity;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

/**
 * @author Cfeng
 * @date 2022/9/16
 */

@Component
@Slf4j
@RequiredArgsConstructor
public class FanoutConsumer {

    private final ObjectMapper objectMapper;

    @RabbitListener(queues = "${spring.rabbitmq.queue-one-name}",containerFactory = "singleListenerContainer")
    public void consumeFanoutMsgOne(@Payload Object messageEntity) {
        try {
            log.info("消息模型fanoutOne 监听到消息：{}", messageEntity);
        } catch (Exception e) {
            log.info("消息模型——消费者one 发生异常： {}",e.fillInStackTrace());
        }
    }

    @RabbitListener(queues = "${spring.rabbitmq.queue-two-name}",containerFactory = "singleListenerContainer")
    public void consumeFanoutMsgTwo(@Payload Object messageEntity) {
        try {
//            MessageEntity messageEntity = objectMapper.readValue(msg,MessageEntity.class);
            log.info("消息模型fanoutTWO 监听到消息：{}", messageEntity);
        } catch (Exception e) {
            log.info("消息模型——消费者two 发生异常： {}",e.fillInStackTrace());
        }
    }
}
